package game.module.hall.processor;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.HallServer;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.hall.bean.SettingBean;
import game.module.hall.dao.SettingCache;
import game.module.hall.dao.SettingDao;
import game.module.hall.logic.SettingDaoHelper;
import game.module.user.logic.PlayerManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lion.netty4.processor.MsgProcessor;
import lua.LuaMessageHall.C2SSendCfg;
import lua.LuaMessageHall.S2CSendCfg;

@MsgCodeAnn(msgcode = C2SSendCfg.id, accessLimit = 500)
public class SettingProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(SettingProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw request) throws Exception {
		// TODO Auto-generated method stub
		C2SSendCfg reqMsg = C2SSendCfg.parse(request);
		int voice = reqMsg.voice;
		int msg = reqMsg.msg;
		int clubInvite = reqMsg.club_invite;
		logger.info("setting,voice={},msg={}", voice, msg);
		// 配置信息是否存在

		SettingBean settingBean = SettingCache.getInstance().getSetting(playingRole.getId());
		if (settingBean == null) {
			settingBean = new SettingBean();
			settingBean.setPlayerId(playingRole.getId());
			if (voice == 1)
				settingBean.setVoice(true);
			else
				settingBean.setVoice(false);
			if (msg == 1)
				settingBean.setMsg(true);
			else
				settingBean.setMsg(false);
			if (clubInvite == 1) {
				settingBean.setClubInvite(true);
			} else {
				settingBean.setClubInvite(false);
			}

			SettingCache.getInstance().addSetting(settingBean);

			SettingDaoHelper.getInstance().asyncAddSetting(settingBean);
		} else {
			if (voice == 1)
				settingBean.setVoice(true);
			else
				settingBean.setVoice(false);
			if (msg == 1)
				settingBean.setMsg(true);
			else
				settingBean.setMsg(false);
			
			if (clubInvite == 1) {
				settingBean.setClubInvite(true);
			} else {
				settingBean.setClubInvite(false);
			}

			SettingDaoHelper.getInstance().asyncUpdateSetting(settingBean);
		}
		// SettingDaoHelper.getInstance().asyncAddSetting(settingBean);

		// ret
		S2CSendCfg response = new S2CSendCfg();
		playingRole.writeAndFlush(response.build(playingRole.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null

	}

}
